package com.yc.tank;

import java.awt.Graphics;
import java.awt.Rectangle;

import com.yc.tank.util.PropertiesTools;

public class Tank {
	private TankFrame tf;
	
	//坦克速度
	private int speed = PropertiesTools.getInt("tankSpeed");
	//坦克位置
	private int x;
	private int y;
	//坦克行进方向
	private Dir dir = Dir.DOWN;
	//坦克是否运行，默认静止（业务要求：坦克要么静止，要么以speed速度运行，speed不改变）
	private boolean isMoving = false;
	//坦克分组
	private Group group;
	//坦克的实质内核（用于碰撞检测）
	private Rectangle rect;
	
	//坦克是否还存活（还没炸毁）
	private boolean living = true;
	//机器大脑
	private MachineBrain brain;
	//是否自动运行
	private boolean auto;
	
	/**
	 * 坦克速度默认10，初始方向向下
	 * @param x
	 * @param y
	 */
	public Tank(int x, int y, Group group, boolean auto, TankFrame tf) {
		super();
		this.x = x;
		this.y = y;
		this.group = group;
		this.auto = auto;
		this.tf = tf;
		if (auto) {
			this.brain = new MachineBrain(tf, this);
			this.isMoving = true;
		}
		rect = new Rectangle(x, y, ResourceMgr.tankLen, ResourceMgr.tankLen);
	}

	public Tank(int speed, int x, int y, Group group, boolean auto, Dir dir) {
		super();
		this.speed = speed;
		this.x = x;
		this.y = y;
		this.group = group;
		this.auto = auto;
		this.dir = dir;
		if (auto) {
			this.brain = new MachineBrain(tf, this);
			this.isMoving = true;
		}
		rect = new Rectangle(x, y, ResourceMgr.tankLen, ResourceMgr.tankLen);
	}
	
	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public Dir getDir() {
		return dir;
	}

	public void setDir(Dir dir) {
		this.dir = dir;
	}

	public boolean isMoving() {
		return isMoving;
	}

	public void setMoving(boolean isMoving) {
		this.isMoving = isMoving;
	}
	

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}
	
	public Rectangle getRect() {
		return rect;
	}

	public void paint(Graphics g) {
		
		if (!living)
			tf.tanks.remove(this);
		
		switch(dir) {
		case LIFT:
			g.drawImage(group.equals(Group.BAD)?ResourceMgr.badTankL:ResourceMgr.goodTankL, x, y, null);
			break;
		case UP:
			g.drawImage(group.equals(Group.BAD)?ResourceMgr.badTankU:ResourceMgr.goodTankU, x, y, null);
			break;
		case RIGHT:
			g.drawImage(group.equals(Group.BAD)?ResourceMgr.badTankR:ResourceMgr.goodTankR, x, y, null);
			break;
		case DOWN:
			g.drawImage(group.equals(Group.BAD)?ResourceMgr.badTankD:ResourceMgr.goodTankD, x, y, null);
			break;
		default:
			break;
					
		}
		
		move();
		
		if (null != brain) {
			//大脑决策（每次刷新屏幕，大脑做一次决策：改变方向，是否发射子弹）
			dir = brain.nextDir();
			if (brain.nextFire())
				fire();
		}
	}
	
	private void move() {
		if (!isMoving) return;
		
		//这里如果用if就显得比较麻烦，每次还需要写“方向==xxx”
		switch(dir) {
		case LIFT://这里的写法，不要写成Dir.LIFT
			x -= speed;
			break;
		case UP:
			y -= speed;
			break;
		case RIGHT:
			x += speed;
			break;
		case DOWN:
			y += speed;
			break;
		}
		rect.setLocation(x, y);//改变坦克灵魂位置
	}

	/**
	 * 坦克开火：增加一个子弹
	 */
	public void fire() {
		int bx = 0;
		int by = 0;
		switch(dir) {
		case LIFT:
		{
			bx = x - 25;
			by = y + ResourceMgr.tankLen/2 - 15;
			break;
		}
		case UP:
		{
			bx = x + ResourceMgr.tankLen/2 - 15;
			by = y - 25;
			break;
		}
		case RIGHT:
		{
			bx = x + ResourceMgr.tankLen - 0;
			by = y + ResourceMgr.tankLen/2 - 15;
			break;
		}
		case DOWN:
		{
			bx = x + ResourceMgr.tankLen/2 - 15;
			by = y + ResourceMgr.tankLen - 0;
			break;
		}
		default:
			break;
		}
		tf.bullets.add(new Bullet(bx, by, this.dir, this.group, this.tf));
		
	}
	
	/**
	 * 销毁
	 */
	public void die() {
		//不要立即删除list的坦克，因为“刷屏碰撞检查” 和 这里的删除是“两个不相干的线程”，如果在
		//在刷屏碰撞检查中删除list里的内容，就会让list可能找不到对象而报错
		//所以要让这两件事形成“同步”
		living = false;
		//显示爆炸效果
		tf.explodes.add(new Explode(tf, x, y));
	}
	
}
